//    Copyright (C) 2013  <lugocreation@gmail.com>
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package org.jpgolay.me.export;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyVetoException;
import javax.swing.*;
import org.jpgolay.gui.fixed.*;
import org.jpgolay.gui.iconBar.*;
import org.jpgolay.me.util.BrowserControl;
import org.jpgolay.me.util.Parameter;

// Referenced classes of package org.jpgolay.me.export:
//            ExportGUI, OLD_ExportCRUD
public class ExportController extends ExportGUI {

    public ExportController(Parameter param) {
        calendarPeriod = null;
        write = false;
        this.param = param;
        desktop = param.getDesktop();
        initLaw();
        initMenu();
        initIconBar();
        initField();
    }

    private void initLaw() {
        if (param.getAccessLaw().length() >= 7 && param.getAccessLaw().charAt(7) == '1') {
            write = true;
        }
    }

    private void initMenu() {
        getMenuPanel().getExit().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionExit();
            }
        });
        getMenuPanel().getDelete().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getHelp().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionHelp();
            }
        });
        getMenuPanel().getModify().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getNew().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getOpen().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getPrint().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent actionevent) {
            }
        });
        getMenuPanel().getSave().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionSave();
            }
        });
    }

    private void initIconBar() {
        getIconBarPanel().addAnswerListener(new AnswerListener() {
            public void create(AnswerEvent answerevent) {
            }

            public void open(AnswerEvent answerevent) {
            }

            public void modify(AnswerEvent answerevent) {
            }

            public void save(AnswerEvent e) {
                actionSave();
            }

            public void remove(AnswerEvent answerevent) {
            }

            public void print(AnswerEvent answerevent) {
            }

            public void help(AnswerEvent e) {
                actionHelp();
            }
        });
    }

    private void initField() {
        getIconBarPanel().setEnabledCreate(false);
        getIconBarPanel().setEnabledSave(write);
        getIconBarPanel().setEnabledModify(false);
        getIconBarPanel().setEnabledRemove(false);
        getIconBarPanel().setEnabledPrint(false);
        getIconBarPanel().setEnabledOpen(false);
        getMenuPanel().getNew().setEnabled(false);
        getMenuPanel().getSave().setEnabled(write);
        getMenuPanel().getModify().setEnabled(false);
        getMenuPanel().getDelete().setEnabled(false);
        getMenuPanel().getPrint().setEnabled(false);
        getMenuPanel().getOpen().setEnabled(false);
        getFileName().setVal("export_compta");
        getCalendarPeriod();
        getPeriod().getButtonRight().addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                actionCalendarPeriod();
            }
        });
    }

    private CalendarPeriod getCalendarPeriod() {
        if (calendarPeriod == null) {
            calendarPeriod = new CalendarPeriod();
            calendarPeriod.setSelectedPeriod(2);
            getPeriod().setLabelLeft(calendarPeriod.getMessage());
            calendarPeriod.getButtonLeft().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriodOK();
                }
            });
            calendarPeriod.getButtonRight().addActionListener(new ActionListener() {
                public void actionPerformed(ActionEvent evt) {
                    actionCalendarPeriodCANCEL();
                }
            });
            calendarPeriod.setLocation(600, 50);
            calendarPeriod.setSize(320, 130);
        }
        return calendarPeriod;
    }

    private void actionExit() {
        setVisible(false);
    }

    private void actionHelp() {
        BrowserControl.displayURL("www.media-4-u.net");
    }

    private void actionSave() {
        OLD_ExportCRUD ex = new OLD_ExportCRUD();
        ex.setUserId(param.getUserId());
        ex.setDateDebut(getCalendarPeriod().getFromDate());
        ex.setDateFin(getCalendarPeriod().getToDate());
        ex.setDestination(getFileName().getVal());
        switch (getExportOn().getSelectedIndex()) {
            case 4: // '\004'
            default:
                break;

            case 0: // '\0'
                if (ex.factureEBP()) {
                    JOptionPane.showMessageDialog(this, "Exportation Reussie !");
                } else {
                    JOptionPane.showMessageDialog(this, "Exportation Refus\351e !");
                }
                break;

            case 1: // '\001'
                if (ex.factureCIEL()) {
                    JOptionPane.showMessageDialog(this, "Exportation Reussie !");
                } else {
                    JOptionPane.showMessageDialog(this, "Exportation Refus\351e !");
                }
                break;

            case 2: // '\002'
                if (ex.factureSARI()) {
                    JOptionPane.showMessageDialog(this, "Exportation Reussie !");
                } else {
                    JOptionPane.showMessageDialog(this, "Exportation Refus\351e !");
                }
                break;

            case 3: // '\003'
                if (ex.factureJCOMPTA()) {
                    JOptionPane.showMessageDialog(this, "Exportation Reussie !");
                } else {
                    JOptionPane.showMessageDialog(this, "Exportation Refus\351e !");
                }
                break;
        }
    }

    private void actionCalendarPeriod() {
        getCalendarPeriod().setVisible(true);
        getCalendarPeriod().toFront();
        desktop.add(getCalendarPeriod(), JLayeredPane.DEFAULT_LAYER);
        try {
            getCalendarPeriod().setSelected(true);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }

    private void actionCalendarPeriodOK() {
        getPeriod().setLabelLeft(getCalendarPeriod().getMessage());
        actionCalendarPeriodCANCEL();
    }

    private void actionCalendarPeriodCANCEL() {
        getCalendarPeriod().setVisible(false);
        desktop.remove(getCalendarPeriod());
    }
    private CalendarPeriod calendarPeriod;
    private JDesktopPane desktop;
    private Parameter param;
    private boolean write;
}
